/**
 * Promise 异常类
 * @author 木人心
 * @version 1.0.0
 */
/**
 * 以Promise的reject被try时可以携带数据到catch中为思路，创建RejectError异常类可以throw出带有数据的类
 */
type Args = [string] | [number, string] | [number, { [p: string]: any }] | [{ code: number, message?: string, data: any }]
export default class RejectError extends Error {
  readonly name: 'RejectError'
  readonly code: number
  readonly message: ''
  readonly data: any

  constructor (...args: Args) {
    const arg = args[0]
    switch (typeof arg) {
      case 'string':
        super(arg); break
      case 'number':
        if (typeof args[1] === 'string') {
          super(args[1])
        } else {
          super()
          this.data = args[1]
        }
        this.code = arg
        break
      case 'object':
        super(arg.message)
        this.code = arg.code
        this.data = arg.data
        break
    }
  }
}
